let brain = require("brain.js");
let fs = require("fs");
let data = require("../res/res.json");
let path = require("path");
let network = new brain.NeuralNetwork({
  inputSize: 16,
  hiddenLayers: [80, 16],
  outputSize: 1,
});

let ii = "";

let trainData = []; // 训练数据
for (let item in data) {
  let o = item - 0 + 1;
  let i = item - 0 - 1;
  if (data[o] && data[i]) {
    let blue = data[item].blue[0];
    let iblue = data[i].blue[0];
    let wzj = data[item].wzjblue;
    // if (blue > iblue) {
    //   wzj.push(1);
    // } else {
    //   wzj.push(0);
    // }
    trainData.push({
      input: wzj,
      output: data[o].blue.map((i) => i / 100),
    });
  }

  // trainData.push(data[item].red);
}

network.train(trainData, {
  // learningRate: 0.1,
  errorThresh: 0.00001,
  iterations: 20000, // 训练迭代次数
  log: true, // 是否打印训练日志
  logPeriod: 500, // 日志打印间隔
  learningRate: 0.05,
  momentum: 0.1,
  // callbackPeriod: 10,
  // timeout: Infinity,
});
const jsonstr = network.toJSON();
// net.fromJSON(json);

fs.writeFile(
  path.join(__dirname, `./model${ii}.json`),
  JSON.stringify(jsonstr, null, 2),
  { flag: "w" },
  () => {}
);

// const output = network.run("CSS flex for example layouts"); // 前端任务：用于复杂布局的 CSS Grid
// const output2 = network.run("optimizing SQL queries"); // 后端任务：优化 SQL 查询
// console.log(output);
// console.log(output2);
